/**
 * Created by Tomkk on 2016/5/2.
 */
angular.module('app.services',[])
    .factory('SystemService',function(){
        var isLogin = false;

        return {
            isLogin: function(){
                return isLogin;
            },
            getHostIp:function(){
                return "http://localhost/final/pro/backend/"
            },
            setLogin:function(bool){
                isLogin = bool;
            }
        }
    })
    .service('LoginService',function($http,SystemService,$q){
        var service = {
            login:login
        }
        return service;

        function login(user){
            var defferd = $q.defer();
            var param = {"user":user};
            $http.post(SystemService.getHostIp()+'login.php',param)
                .success(function(data, status, headers, config){
                    defferd.resolve(data);
                    window.sessionStorage.isLogin = true;
                    window.sessionStorage.isFirst = true;
                }).error(function (data, status, headers, config) {
                    deferred.reject(data);
                });
            return defferd.promise;
            }
        })
    .service('RegisterService',function($q,$http,SystemService){
        var service = {
            register:register
        }
        return service;

        function register(user){
            var deferred = $q.defer();
            var param = {"user":user};
            $http.post(SystemService.getHostIp()+'register.php',param)
                .success(function(data, status, headers, config){
                    if(data.code == 0){
                        deferred.resolve(data);
                    }
                }).error(function (data, status, headers, config) {
                        deferred.reject(data);
            });
            return deferred.promise;
        }
    })
    .service('ListInfoService',function($q,$http,SystemService){
        var students = [];
        var allStudents = [];
        var isChange = true;
        var currentUser = {};
        var service = {
            getInfo:getInfo,
            setIsChange:setIsChange,
            setCurrentStudent:setCurrentStudent,
            getCurrentStudent:getCurrentStudent,
            getShowStudent:getShowStudent,
            setUnUse:setUnUse
        }
        return service;

        function setUnUse(index){
            var defferd = $q.defer();
            var param = {user_id:allStudents[index].user_id};
            $http.post(SystemService.getHostIp()+'delete.php',param)
                .success(function(data, status, headers, config){
                if(data.code == 0){
                    defferd.resolve(data);
                }
            }).error(function (data, status, headers, config) {
                deferred.reject(data);
            });
            return defferd.promise;
        }
        function getShowStudent(index){
            return allStudents[index];
        }
        function getCurrentStudent(){
            return currentUser;
        }
        function setCurrentStudent(index){
            currentUser = allStudents[index];
            return ;
        }
        function setIsChange(){
            isChange = true;
        }
        function getInfo(){
            var defferd = $q.defer();
            if(!isChange){
                defferd.resolve(students);
            }else {
                $http.get(SystemService.getHostIp() + 'getInfo.php').success(function (data, status, headers, config) {
                    console.log(data);
                    allStudents = data.returnValue;
                    students = [];
                    allStudents.forEach(function(item){
                        if(!item.image){
                            item.image = "http://localhost/final/pro/img/guest.png";
                        }
                            var student = {};
                            student.real_name = item.real_name;
                            student.card_num = item.card_num;
                            student.enter_year = item.enter_year;
                            student.class_name = item.class_name;
                            student.mobile = item.mobile;
                            student.user_isuse = item.user_isuse;
                            students.push(student);

                    });
                    isChange = false;
                    defferd.resolve(students);
                }).error(function (data, status, headers, config) {
                    defferd.reject(data);
                })
            }
            return defferd.promise;
        }
    })
    .service('EditUserService',function($q,$http,ListInfoService,SystemService){
        var currentUser = {};
        var service = {
            editUser:editUser
        }
        return service;

        function editUser(user){
            var defferd = $q.defer();
            var param = {user:user};
            $http.post(SystemService.getHostIp()+"/update.php",param).success(function(data,status,headers,config){
                if(data.code == 0){
                    defferd.resolve(data.returnValue);
                    ListInfoService.setIsChange();
                }
            }).error(function (data, status, headers, config) {
                defferd.reject(data);
            });
            return defferd.promise;
        }
    })